package com.wzdigit.wms.wms.service.report;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.report.PeakReportRequest;
import com.wzdigit.wms.basic.client.response.asn.other.OtherInOrderInfoResponse;
import com.wzdigit.wms.basic.client.response.report.PeakReportResponse;
import com.wzdigit.wms.basic.domain.dtos.report.WarehousePeakDto;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.mapper.core.inventory.GoodsMapper;
import com.wzdigit.wms.common.entity.ExcelExportConfig;
import com.wzdigit.wms.common.utils.HttpExcel;
import com.wzdigit.wms.wms.domain.WarehousePeak;
import com.wzdigit.wms.wms.mapper.WarehousePeakMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WarehousePeakService {
    @Autowired
    WarehousePeakMapper warehousePeakMapper ;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    WarehouseMapper warehouseMapper ;
    @Transactional
    public  void  saveWarehousePeak(){
        Date curDate = new Date();
        log.info("开始保存仓库库存，当前时间=》"+curDate);
        //获取当前库存goods 数据
        List<WarehousePeakDto> warehousePeakDto =  goodsMapper.selectCurWarehousePeak();
        //插入库存峰值表
        for(WarehousePeakDto item : warehousePeakDto){
            WarehousePeak warehousePeak = new WarehousePeak();
            warehousePeak.setCreateUser("system");
            warehousePeak.setCreateTime(curDate);
            warehousePeak.setCurQty(item.getCurQty());
            warehousePeak.setWhsId(item.getWhsId());
            warehousePeakMapper.insert(warehousePeak);
        }
    }

    //查询仓库峰值报表
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public PagerInfo<List<PeakReportResponse>> searchWhsPeakReport(
            PeakReportRequest search, Pager<?> pager){

        if (search.getFilter()!=null){
            search.setFilter(search.getFilter().replace(" ",""));
        }
        //根据前端传的long毫秒值设置时间
        if (search.getBeginTime()!=null){
            search.setBeginTimeDate(new Date(search.getBeginTime()));
        }
        if (search.getEndTime()!=null){
            search.setEndTimeDate(new Date(search.getEndTime()));
        }
        Page<PeakReportResponse> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        List<PeakReportResponse> datas = warehousePeakMapper.selectWhsReport(search);
        PagerInfo<List<PeakReportResponse>> pagerInfo = new PagerInfo<>();
        pagerInfo.setTotalCount(page.getTotal());
        pagerInfo.setItems(datas);

        return pagerInfo;
    }

    //导出仓库峰值报表
    @Transactional
    public void exportWhsPeakReport(
            PeakReportRequest search,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        ServletOutputStream out = null;
        XSSFWorkbook workbook = null;
        try {
            if (search.getFilter()!=null){
                search.setFilter(search.getFilter().replace(" ",""));
            }
            //根据前端传的long毫秒值设置时间
            if (search.getBeginTime()!=null){
                search.setBeginTimeDate(new Date(search.getBeginTime()));
            }
            if (search.getEndTime()!=null){
                search.setEndTimeDate(new Date(search.getEndTime()));
            }
            List<PeakReportResponse> resultDatas = warehousePeakMapper.selectWhsReport(search);

            List<ExcelExportConfig> configs = new ArrayList<>();
            configs.add(new ExcelExportConfig("日期", "dayTime", 120));
            configs.add(new ExcelExportConfig("仓库", "whsCode", 120));
            configs.add(new ExcelExportConfig("仓库名称", "whsName", 120));
            configs.add(new ExcelExportConfig("峰值库存数量", "qty", 120));
            configs.add(new ExcelExportConfig("单位", "unit", 120));
            workbook = HttpExcel.newListToExcel(resultDatas, configs);
            String userAgent = servletRequest.getHeader("USER-AGENT");
            String fileName = "物料峰值报表";
            if (userAgent.contains("Mozilla")) {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "utf8");
            }
            servletResponse.setContentType("application/octet-stream");
            servletResponse.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
            servletResponse.flushBuffer();
            out = servletResponse.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if(workbook != null) {
                workbook.close();
            }
        }

    }


}
